<!--
* @description 安全评分设置
* @author zywu
* @date 2021/04/21 10:05:05
!-->
<template>
  <div>
    <el-table v-loading="loading" :data="tableData" ref="mainTable" style="width: 100%">
      <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
      <el-table-column prop="name" label="评估项" align="center"></el-table-column>
      <el-table-column prop="deductMax" label="扣分上限" width="120" align="center"></el-table-column>
      <el-table-column prop="evaluateContent" label="表达式"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="text" size="small">设置</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :close-on-click-modal="false"
      title="安全评估配置"
      width="600px"
      top="10vh"
      v-if="dialogFlag"
      :visible.sync="dialogFlag"
      v-dialogDrag
      @close="closeDialog"
    >
      <el-form ref="dataForm" :model="dataForm" :rules="rules" label-width="100px" @submit.native.prevent>
        <el-form-item label="评估项" prop="name">
          <el-input type="text" v-model="dataForm.name" readonly></el-input>
        </el-form-item>
        <el-form-item label="扣分上限" prop="deductMax">
          <el-input-number ref="maxInput" v-model="dataForm.deductMax" :controls="false" :min="0"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFlag = false">取消</el-button>
        <el-button type="primary" @click="handleSave">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
import { getOrgSafetyRule, configSafetyRule } from '@/api/sysConfig'
export default {
  data() {
    return {
      loading: false,
      tableData: [],
      dialogFlag: false,
      dataForm: {
        name: '',
        deductMax: '',
      },
      rules: {},
    }
  },

  components: {},

  computed: {
    ...mapState(['userinfo']),
    ...mapGetters(['userOrgId']),
  },

  created() {
    this.getList()
  },

  mounted() {},

  methods: {
    getList() {
      this.loading = true
      getOrgSafetyRule({
        orgId: this.userOrgId,
      })
        .then((res) => {
          this.tableData = res.data.safetyRuleDetailsList
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    handleSave() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          let rule = {}
          let { deductMax, ruleType } = this.dataForm
          rule[ruleType] = {
            deductMax: !deductMax && deductMax != 0 ? null : deductMax,
          }
          configSafetyRule({
            orgId: this.userOrgId,
            account: this.userinfo.accountName,
            rules: JSON.stringify(rule),
          }).then((res) => {
            if (res.status == 200) {
              this.$message.success('操作成功')
              this.closeDialog()
              this.getList()
            } else {
              this.$message.error(res.info)
            }
          })
        }
      })
    },
    handleEdit(row) {
      this.dialogFlag = true
      this.dataForm = Object.assign({}, row)
      if (row.deductMax == null) {
        this.$nextTick(() => {
          this.$refs.maxInput.$refs.input.clear()
        })
      }
    },
    closeDialog() {
      this.dialogFlag = false
    },
  },
}
</script>